﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Extensions.DependencyInjection;
using SpCad;
using SPCAM.Plugin.Cam.Model;
using SPCAM.Plugin.Cam.ViewModel;
using SPCAM.Plugin.Cam.Views;
namespace SPCAM.Plugin.Cam.Views
{
    /// <summary>
    /// WeldTree.xaml 的交互逻辑
    /// </summary>
    public partial class WeldTree : UserControl
    { 
        public WeldTree( )
        {
            InitializeComponent();
            DataContext =  App.Current.Services.GetService<WeldTreeViewModel>();
            SpServer.Instance.Current.PropertyChanged += Current_PropertyChanged;
        }

        private void Current_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if(e.PropertyName== "Document")
            {
                var dcon = this.DataContext as WeldTreeViewModel;
                dcon?.TreeNode.Clear();
            }
        }

     

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
           var dcon=   this.DataContext as WeldTreeViewModel;
            dcon.SelectWeld =(WeldTreeModel) e.NewValue;
            
        }

     /// <summary>
     /// 获取到焦点事件
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
        private void TreeView_GotFocus(object sender, RoutedEventArgs e)
        {
            var dcon = this.DataContext as WeldTreeViewModel;
            dcon.SelectWeld = (WeldTreeModel)mtreeview.SelectedItem;
        }
        /// <summary>
        /// 失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeView_LostFocus(object sender, RoutedEventArgs e)
        {
            return;
            var dcon = this.DataContext as WeldTreeViewModel;
            dcon.SelectWeld = null;
        }
    }
}
